package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.pojo.Car;
import com.dk.pojo.Type;
import com.dk.service.CarService;
import com.dk.service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("car")
public class CarController {
    @Reference
    private CarService carService;
    @Reference
    private TypeService typeService;
    @RequestMapping("toShow")
    public String toShow(Model model){
        List<Type> list = typeService.selectInfo();
        model.addAttribute("list",list);
        return "car/show";
    }
    @RequestMapping("getInfo")
    @ResponseBody
    public List<Car> getInfo(Car car){
        List<Car> list = carService.getInfo(car);
        return list;
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "car/add";
    }
    @RequestMapping("increase")
    @ResponseBody
    public boolean increase(Car car){
        return  carService.insert(car) > 0;
    }
    @RequestMapping("toUpdate/{cid}")
    public String toUpdate(@PathVariable("cid")Integer cid, Model model){
       Car car = carService.selectByCid(cid);
        model.addAttribute("car",car);
        return "car/update";
    }
    @RequestMapping("updateInfo")
    @ResponseBody
    public boolean updateInfo(Car car){
       return carService.updaateInfo(car) > 0;
    }
    @RequestMapping("deleteById")
    @ResponseBody
    public boolean deleteById(Integer cid){
        return carService.deleteById(cid) > 0;
    }
}
